package com.bocai.eelly.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.bocai.eelly.R;
import com.bocai.eelly.adapter.HistoryCaiGAdp;
import com.bocai.eelly.bean.HistoryListBean;
import com.bocai.eelly.common.BaseFragment;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.Utils;
import com.bocai.eelly.views.XListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class HistoryCGFragment extends BaseFragment implements XListView.IXListViewListener,AdapterView.OnItemClickListener{
    @Bind(R.id.lv_find_stock)
    XListView lvFindStock;
    private View view;
    private int page = 1;
    private List<HistoryListBean.DataEntity> dataEntities=new ArrayList<>();
    private HistoryCaiGAdp historyCaiGAdp;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_find_stock, null);
        initView();
        return view;

    }

    private void initView() {
        ButterKnife.bind(this, view);
        getHistoryList();
        historyCaiGAdp=new HistoryCaiGAdp(getActivity(),dataEntities);
        lvFindStock.setAdapter(historyCaiGAdp);
        initEvent();
    }

    private void initEvent() {
        lvFindStock.setXListViewListener(this);
        lvFindStock.setPullLoadEnable(false);
        lvFindStock.setPullRefreshEnable(true);
        lvFindStock.setOnItemClickListener(this);
    }
    private void getHistoryList() {
        Loading.show(getActivity(),getString(R.string.str_loading));
        String timeline = Utils.getTimeline();
        String sign = Utils.md5(timeline);
        getTestApi().getHistoryList(timeline, sign, Utils.getUserId(getActivity(), "id"), page, Utils.PAGESIZE)
                .compose(this.<HistoryListBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HistoryListBean>() {
                    @Override
                    public void onCompleted() {

                    }


                    @Override
                    public void onError(Throwable e) {
                        lvFindStock.stopRefresh();
                        Loading.dismiss();
                        L.d(e.toString());
                        T.showLong(getActivity(),R.string.net_error);
                    }

                    @Override
                    public void onNext(HistoryListBean historyListBean) {
                        L.d(historyListBean.getMsg());
                        lvFindStock.stopRefresh();
                        Loading.dismiss();
                        if (Utils.checkResultCode(historyListBean.getReturn_code())) {
                            dataEntities.addAll(historyListBean.getData());
                            historyCaiGAdp.notifyDataSetChanged();
                            if (historyListBean.getData().size() < Utils.PAGESIZE) {
                                lvFindStock.setPullLoadEnable(false);
                            }else{
                                lvFindStock.setPullLoadEnable(true);
                            }
                        }else{
                            T.showShort(getActivity(),historyListBean.getMsg());
                        }
                    }
                });

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onRefresh() {
        page=1;
        dataEntities.clear();
        lvFindStock.setPullLoadEnable(false);
        getHistoryList();
    }

    @Override
    public void onLoadMore() {
        page++;
        getHistoryList();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
